package jcu.ict.profaid;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint({ "NewApi", "SimpleDateFormat" })
public class FormAddCourse extends Activity {
	@SuppressLint("NewApi")
	private static CourseDbAdapter cdb;
	private String University;
	private String Program;
	private String Ccode;
	private String Cname;
	private String Year;
	private String Notes;
	private String i;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.form_add_subject);
		
		Bundle bundle = this.getIntent().getExtras();
		i = (String) bundle.getCharSequence("setUserId");

		final EditText edt_university = (EditText) findViewById(R.id.edtUniversity);
		final EditText edt_program = (EditText) findViewById(R.id.edtProgram);
		final EditText edt_Ccode = (EditText) findViewById(R.id.edtCourseCode);
		final EditText edt_Cname = (EditText) findViewById(R.id.edtCourseName);
		final EditText edt_notes = (EditText) findViewById(R.id.edtNotes);
		final RadioButton rbnUniv = (RadioButton) findViewById(R.id.rbnUniversity);
		final RadioButton rbnCon = (RadioButton) findViewById(R.id.rbnConferences);

		final TextView txt1 = (TextView) findViewById(R.id.txt1);
		final TextView txt2 = (TextView) findViewById(R.id.txt2);
		final TextView txt3 = (TextView) findViewById(R.id.txt3);
		final TextView txt4 = (TextView) findViewById(R.id.txt4);
		final TextView txt5 = (TextView) findViewById(R.id.txt5);

		cdb = new CourseDbAdapter(getBaseContext());
		Button btnSave = (Button) findViewById(R.id.btnYes);
		Button btnClose = (Button) findViewById(R.id.btnNo);

		University = edt_university.getText().toString();
		Program = edt_program.getText().toString();
		Ccode = edt_Ccode.getText().toString();
		Cname = edt_Cname.getText().toString();

		rbnUniv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				edt_university.setError(null);
				edt_program.setError(null);
				edt_Ccode.setError(null);
				edt_Cname.setError(null);

				txt1.setVisibility(View.VISIBLE);
				txt2.setVisibility(View.VISIBLE);
				txt3.setVisibility(View.VISIBLE);
				txt4.setVisibility(View.VISIBLE);
				txt5.setVisibility(View.VISIBLE);

				txt1.setText("University");
				txt2.setText("Program");

				edt_university.setVisibility(View.VISIBLE);
				edt_program.setVisibility(View.VISIBLE);
				edt_Cname.setVisibility(View.VISIBLE);
				edt_Ccode.setVisibility(View.VISIBLE);
				edt_notes.setVisibility(View.VISIBLE);
			}
		});

		rbnCon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edt_university.setError(null);
				edt_program.setError(null);
				
				txt1.setVisibility(View.VISIBLE);
				txt2.setVisibility(View.VISIBLE);
				txt3.setVisibility(View.INVISIBLE);
				txt4.setVisibility(View.INVISIBLE);
				txt5.setVisibility(View.INVISIBLE);

				txt1.setText("Company*");
				txt2.setText("Session*");

				edt_university.setVisibility(View.VISIBLE);
				edt_program.setVisibility(View.VISIBLE);
				edt_Cname.setVisibility(View.INVISIBLE);
				edt_Ccode.setVisibility(View.INVISIBLE);
				edt_notes.setVisibility(View.INVISIBLE);
			}
		});

		btnSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (rbnUniv.isChecked()) {
					University = edt_university.getText().toString();
					Program = edt_program.getText().toString();
					Ccode = edt_Ccode.getText().toString();
					Cname = edt_Cname.getText().toString();

					if (University.equalsIgnoreCase("") || Program.equalsIgnoreCase("")
							|| Ccode.equalsIgnoreCase("") || Cname.equalsIgnoreCase("")) {
						
						if (University.equals("")) {
							edt_university.setError("Enter University name");
						}

						if (Program.equals("")) {
							edt_program.setError("Enter a program");
						}

						if (Ccode.equals("")) {
							edt_Ccode.setError("Enter a cource code");
						}

						if (Cname.equals("")) {
							edt_Cname.setError("Enter a course name");
						}
												
						Toast.makeText(getBaseContext(),
								"Please fill all require field",
								Toast.LENGTH_LONG).show();
											
						
					} else {
						Calendar c = Calendar.getInstance();
						SimpleDateFormat df1 = new SimpleDateFormat(
								"dd-MMM-yyyy");
						Year = df1.format(c.getTime());
						Notes = edt_notes.getText().toString();

						edt_university.setError(null);
						edt_program.setError(null);
						edt_Ccode.setError(null);
						edt_Cname.setError(null);
						
						cdb.open();
						cdb.insertCourseList(i,University, Program, Ccode, Cname,
								Year, Notes);
						cdb.close();

						Intent in = new Intent(FormAddCourse.this,
								MainCourse.class);
						Bundle bundles = new Bundle();
						bundles.putCharSequence("setUserId", i);
						in.putExtras(bundles);
						startActivity(in);
						finish();
					}
				}
				if (rbnCon.isChecked()) {
					String Company = edt_university.getText().toString();
					String Session = edt_program.getText().toString();

					if (Company.matches("") || Session.matches("")) {
						
						if (University.equals("")) {
							edt_university.setError("Enter Company name");
						}

						if (Program.equals("")) {
							edt_program.setError("Enter a session");
						}
						
						
						Toast.makeText(getBaseContext(),
								"Please fill all require field",
								Toast.LENGTH_LONG).show();
					} else {
						Calendar c = Calendar.getInstance();
						SimpleDateFormat df1 = new SimpleDateFormat(
								"dd-MMM-yyyy");
						Year = df1.format(c.getTime());
						Notes = edt_notes.getText().toString();

						edt_university.setError(null);
						edt_program.setError(null);
						
						cdb.open();
						cdb.insertCourseList(i,"Conferences", "0", Company, Session, Year,
								Notes);
						cdb.close();

						Intent in = new Intent(FormAddCourse.this,
								MainCourse.class);
						Bundle bundles = new Bundle();
						bundles.putCharSequence("setUserId",i);
						in.putExtras(bundles);
						startActivity(in);
						finish();
					}
				}
			}
		});

		btnClose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent in = new Intent(FormAddCourse.this, MainCourse.class);
				Bundle bundles = new Bundle();
				bundles.putCharSequence("setUserId", i);
				in.putExtras(bundles);
				startActivity(in);
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainCourse.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Bundle bundles = new Bundle();
			bundles.putCharSequence("setUserId", i);
			intent.putExtras(bundles);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
